package com.bobi.tfca.ui.mainnavigation.user.apply

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.os.Bundle
import android.widget.TextView
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.databinding.FragmentMemEntryApplyBinding
import com.bobi.tfca.network.response.AuditResponse
import com.bobi.tfca.network.response.MemberResponse
import com.bobi.tfca.ui.adapter.EntryApplyAdapter

class MemEntryApplyFragment : BaseFragment<FragmentMemEntryApplyBinding, MemApplyFragmentVM>(
    FragmentMemEntryApplyBinding::inflate,
    MemApplyFragmentVM::class.java,
    true
) {
    private lateinit var applyRV: RecyclerView

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentMemEntryApplyBinding,
        viewModel: MemApplyFragmentVM?,
        savedInstanceState: Bundle?
    ) {
        // 初始化RV
        initRecyclerView(binding, viewModel!!)

        // 监听viewmodel
        viewModel.apply {
            audits.observe(this@MemEntryApplyFragment) {
                val adapter = applyRV.adapter as EntryApplyAdapter
                adapter.setItemList(it)
                adapter.notifyDataSetChanged()
            }
        }

    }

    /**
     * 初始化RV
     */
    private fun initRecyclerView(
        binding: FragmentMemEntryApplyBinding,
        viewModel: MemApplyFragmentVM
    ) {
        // 获取RecyclerView实例
        applyRV = binding.rvMemberApply

        // 获得所有活动
        val auditList: ArrayList<AuditResponse> = arrayListOf()
        // 设置布局管理器
        val linearLayoutManager = LinearLayoutManager(binding.root.context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        applyRV.layoutManager = linearLayoutManager
        applyRV.addItemDecoration(
            DividerItemDecoration(
                binding.root.context,
                LinearLayoutManager.VERTICAL
            )
        )
        val auditAdapter = EntryApplyAdapter(auditList)

        // 设置监听器
        auditAdapter.setOnDetailsClickListener(object : EntryApplyAdapter.OnItemClickListener {
            override fun onItemClick(position: Int) {
                // 设置跳转详情
                val member = MemberResponse()
                member.memberId = auditAdapter.auditList[position].itemId
                publicViewModel!!.member.value = member
                requireActivity().findNavController(R.id.app_nav)
                    .navigate(R.id.action_memberApplyFragment_to_memberProfileFragment)
            }
        })
        // 设置通过按钮监听
        auditAdapter.setOnPassBtClickListener(object : EntryApplyAdapter.OnButtonClickListener {
            override fun onMultiBtClick(position: Int) {
                // 创建确认框
                val builder = AlertDialog.Builder(requireContext())
                builder.setTitle("通过")
                builder.setMessage("你确认通过该会员的入会申请吗")

                // 设置确认按钮和点击事件
                builder.setPositiveButton("确认") { dialog, which ->
                    val auditId = auditAdapter.auditList[position].auditId
                    viewModel.passAudit(publicViewModel!!, auditId, this@MemEntryApplyFragment)
                }

                // 设置取消按钮和点击事件
                builder.setNegativeButton("取消") { dialog, which ->
                }

                // 创建并显示确认框
                val dialog: AlertDialog = builder.create()
                dialog.show()
            }
        })

        // 设置拒绝按钮监听
        auditAdapter.setOnRefuseBtClickListener(object : EntryApplyAdapter.OnButtonClickListener {
            override fun onMultiBtClick(position: Int) {
                // 创建确认框
                val builder = AlertDialog.Builder(requireContext())
                builder.setTitle("拒绝")
                builder.setMessage("你确认拒绝该会员的入会申请吗")

                // 设置确认按钮和点击事件
                builder.setPositiveButton("确认") { dialog, which ->
                    val auditId = auditAdapter.auditList[position].auditId
                    viewModel.refuseAudit(publicViewModel!!, auditId, this@MemEntryApplyFragment)
                }

                // 设置取消按钮和点击事件
                builder.setNegativeButton("取消") { dialog, which ->
                }

                // 创建并显示确认框
                val dialog: AlertDialog = builder.create()
                dialog.show()
            }
        })
        // 绑定Adapter
        applyRV.adapter = auditAdapter
    }

    override fun resumeFragment(viewModel: MemApplyFragmentVM?) {
        super.resumeFragment(viewModel)
        viewModel!!.findAllEntryApply(publicViewModel!!, this)
    }
}